package com.zhongshengnetwork.rightbe.lang.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.DateUtil;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.loading.KyLoadingBuilder;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.dbmodel.NotifydbModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SmartRankCoverModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SmartRankModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SmartRankMyInfo;
import com.zhongshengnetwork.rightbe.gsonmodel.SmartRankShareModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.my.activity.SettingsActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmartRankActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;
    private RefreshLayout mRefreshLayout;
    private ListView listView;
    private MyAdapter adapter = null;
    private int pageindex = 0;
    private static final int Records = 20;
    private List<SmartRankModel> list;

    private SmartRankCoverModel smartRankCoverModel = null;
    private SmartRankShareModel smartRankShareModel = null;
    private SmartRankMyInfo smartRankMyInfo = null;

    private float density = 0;
    private int imgW = 0;
    private int imgH = 0;

    private String rankid;
    private int type = - 1;
    private boolean enterLikeList = false;
    private KyLoadingBuilder load = null;

    private void calculateImageWidth(){
        density = ScreenUtils.getScreenDensity(CustomApplication.mContext);
        imgW = ScreenUtils.getScreenWidth(CustomApplication.mContext);
        imgH = (int) (imgW*10.0f/16.0f);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        calculateImageWidth();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        calculateImageWidth();

        rankid = getIntent().getStringExtra("rankid");
        type = getIntent().getIntExtra("type", - 1);
        enterLikeList = false;
        if (type == 0){
            //占领封面
        }else if (type == 1){
            //赞了封面
        }else if (type == 2){
            //赞了排名
            enterLikeList = true;
        }

        setContentView(R.layout.activity_smart_rank);

        topbar = (TopBarView)findViewById(R.id.topbar);

        if (CommonUtils.isEmpty(rankid)){
            topbar.getTitleView().setText("今日智慧排行");
        }

        topbar.getRightView().setVisibility(View.GONE);
        topbar.getRightView().setImageResource(R.drawable.more);
        topbar.getRightView().setClickable(true);
        topbar.getRightView().setVisibility(View.VISIBLE);
        topbar.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (smartRankCoverModel != null){
                    more(v);
                }else {
                    ToastUtil.show(SmartRankActivity.this, "加载中...");
                }

            }
        });
        topbar.setClickListener(this);

        if (list == null){
            list = new ArrayList<SmartRankModel>();
        }
        listView = (ListView) findViewById(R.id.rank_listview);
        if (adapter == null){
            adapter = new MyAdapter();
        }
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (i == 0){
                    return;
                }

                i -= 1;
                while (i < 0){//解决部分手机点击第一条不是从1开始的问题
                    i+=1;
                }

                 if (list != null && list.size() > i){
                     SmartRankModel model = list.get(i);
                     //进入个人主页
                     Intent intent = new Intent(SmartRankActivity.this, UserLangActivity.class);
                     intent.putExtra(APIKey.useridKey,model.getUserid());
                     intent.putExtra(APIKey.typeKey,1);
                     intent.putExtra(APIKey.titleKey,model.getNickname());
                     startActivity(intent);
                 }
            }
        });

        listView.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
        listView.setDivider(new ColorDrawable(AppThemeUtils.getInstance().getBackgroundColor()));
        listView.setDividerHeight((int)(AppThemeUtils.DividerHeight* ScreenUtils.getScreenDensity(CustomApplication.mContext)));

        RelativeLayout smart_rank_layout = (RelativeLayout) findViewById(R.id.smart_rank_layout);
        smart_rank_layout.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());

        mRefreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        //设置 Header 为 贝塞尔雷达 样式
        mRefreshLayout.setRefreshHeader(new BezierRadarHeader(SmartRankActivity.this).setEnableHorizontalDrag(true));//BezierRadarHeader
        //设置 Footer 为 球脉冲 样式
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(SmartRankActivity.this).setSpinnerStyle(SpinnerStyle.FixedBehind));//BallPulseFooter ClassicsFooter
        mRefreshLayout.setPrimaryColors(AppThemeUtils.getInstance().getRefreshBackgroundColor(),AppThemeUtils.getInstance().getRefreshAccessColor());
        mRefreshLayout.setEnableRefresh(true);//启用刷新
        mRefreshLayout.setEnableLoadMore(false);//启用加载
        //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh();
                pageindex = 0;
                getData();
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //refreshLayout.finishLoadMore();
                getData();
            }
        });
        mRefreshLayout.autoRefresh();

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        finish();
        //Toast.makeText(LangDetailActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();

    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private void more(View view){
        final List<String> menu = new ArrayList<String>();
        menu.add("分享");
        menu.add("关闭智慧排行");
        menu.add("智慧排行说明");

        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(SmartRankActivity.this).builder()
                .setTitle("")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);

        for (String m : menu){
            actionSheetDialog.addSheetItem(m, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which == 1){
                        if (smartRankMyInfo == null){
                            return;
                        }
                        Intent intent = new Intent(SmartRankActivity.this, SmartRankShareActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("model", smartRankMyInfo);//序列化
                        intent.putExtras(bundle);//发送数据
                        startActivity(intent);
                    }else if (which == 2){
                       closeSmartRank();
                    }else if (which == 3){
                        Intent webIntent = new Intent(SmartRankActivity.this, MyWebActivity.class);
                        webIntent.putExtra(APIKey.urlKey, Constant.HOST+"api/system/smartrank.do");
                        startActivity(webIntent);
                    }
                }
            });
        }

        actionSheetDialog.show();

    }

    private void closeSmartRank(){
        CustomDialog dialog = null;
        final CustomDialog.Builder builder = new CustomDialog.Builder(SmartRankActivity.this);
        builder.setTitle("温馨提示");
        builder.setMessage("亲，关闭智慧排行后，你将不再参与每日排名，也不能查看相关的排名信息，如需关闭请按确定。");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                load = new KyLoadingBuilder(SmartRankActivity.this);
                load.setIcon(R.drawable.loading04);
                load.setText("关闭中...");
                load.setOutsideTouchable(false);//点击空白区域是否关闭
                load.setBackTouchable(true);//按返回键是否关闭
                load.show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final Map<String,String> map = new HashMap<String, String>();
                        map.put("dataStatus","0");
                        map.put("token",CustomApplication.loginModel.getToken());
                        String url = "rank/smart/settting.do";
                        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                            @Override
                            public void onSuccess(String data) {
                                CommonModel commonModel = GsonTools.getCommonModel(data);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        load.dismiss();
                                        if (commonModel.getFlag().equals("1")){
                                            ToastUtil.show(SmartRankActivity.this, "关闭成功");
                                            SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.Smart_Rank_State, false);
                                            finish();
                                        }else {
                                            CustomApplication.showTip(commonModel, SmartRankActivity.this);
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onError(String msg) {
                               runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       load.dismiss();
                                   }
                               });
                            }
                        });
                    }
                }).start();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    private void getData(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                final Map<String,String> map = new HashMap<String, String>();
                map.put("pageIndex",pageindex+"");
                map.put("pageRecord",Records+"");
                map.put("dataId", CommonUtils.formatString(rankid));
                if (CommonUtils.isLogin() == true){
                    map.put("token",CustomApplication.loginModel.getToken());
                }
                String url = "rank/smart/list.do";

                HttpsUtils.miniAppDo(map, url,  new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        //listview.stopLoadMore();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                    mRefreshLayout.finishRefresh();
                                }else if (mRefreshLayout.getState() == RefreshState.Loading){
                                    mRefreshLayout.finishLoadMore();
                                }

                                CommonModel commonModel = GsonTools.getCommonModel(data);
                                if (commonModel.getFlag().equals("1")){

                                    if (pageindex == 0){
                                        smartRankCoverModel = GsonTools.getSmartRankCoverModel(data);
                                        smartRankShareModel = GsonTools.getSmartRankShareModel(data);
                                        smartRankMyInfo = GsonTools.getSmartRankMyInfo(data);
                                        if (smartRankMyInfo != null){
                                            rankid = smartRankMyInfo.getRankid();
                                            if (enterLikeList == true){
                                                enterLikeList = false;
                                                SmartRankModel model = new SmartRankModel();
                                                model.setRankid(smartRankMyInfo.getRankid());
                                                Intent intent = new Intent(SmartRankActivity.this, SmartRankLikeActivity.class);
                                                Bundle bundle=new Bundle();
                                                bundle.putSerializable("model", model);//序列化
                                                intent.putExtras(bundle);//发送数据
                                                startActivity(intent);
                                            }
                                        }
                                    }

                                    pageindex += 1;
                                    List<SmartRankModel> l = GsonTools.getSmartRankModel(data);
                                    if (l != null && l.size() > 0){
                                        if (l.size() >= Records){
                                            //listview.setPullLoadEnable(true);
                                            mRefreshLayout.setEnableLoadMore(true);
                                        }else {
                                            //listview.setPullLoadEnable(false);
                                            mRefreshLayout.setEnableLoadMore(false);
                                        }
                                        if (pageindex == 1){
                                            list.clear();
                                        }
                                        list.addAll(l);
                                    }else {
                                        //listview.setPullLoadEnable(false);
                                        mRefreshLayout.setEnableLoadMore(false);
                                    }
                                    adapter.notifyDataSetChanged();
                                }else {
                                    CustomApplication.showTip(commonModel, SmartRankActivity.this);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(String msg) {
                        //listview.stopLoadMore();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                    mRefreshLayout.finishRefresh();
                                }else if (mRefreshLayout.getState() == RefreshState.Loading){
                                    mRefreshLayout.finishLoadMore();
                                }

                            }
                        });
                    }
                });
            }
        }.start();
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size()+1;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position-1);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0){
                return 0;
            }
            return 1;
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //convertView的复用
            View view = null;
            int type = getItemViewType(position);
            if (type == 0){
                if (convertView == null){
                    view = View.inflate(SmartRankActivity.this, R.layout.smart_rank_header,null);
                }else {
                    view = convertView;
                }
                RelativeLayout my_info_header_layout = (RelativeLayout) view.findViewById(R.id.my_info_header_layout);
                my_info_header_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

                RelativeLayout cover_user = (RelativeLayout) view.findViewById(R.id.cover_user);
                cover_user.setClickable(true);
                cover_user.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (smartRankCoverModel != null){
                            //进入个人主页
                            Intent intent = new Intent(SmartRankActivity.this, UserLangActivity.class);
                            intent.putExtra(APIKey.useridKey,smartRankCoverModel.getUserid());
                            intent.putExtra(APIKey.typeKey,1);
                            intent.putExtra(APIKey.titleKey,smartRankCoverModel.getName());
                            startActivity(intent);
                        }
                    }
                });

                RelativeLayout my_info = (RelativeLayout) view.findViewById(R.id.my_info);
                my_info.setClickable(true);
                my_info.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(SmartRankActivity.this, SmartRankShareActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("model", smartRankMyInfo);//序列化
                        intent.putExtras(bundle);//发送数据
                        startActivity(intent);
                    }
                });

                ImageView cover = (ImageView) view.findViewById(R.id.cover);
                ViewGroup.LayoutParams params = cover.getLayoutParams();
                params.width =imgW;
                params.height = imgH;
                cover.setLayoutParams(params);

                cover.setClickable(true);
                cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (smartRankCoverModel != null){
                            clickCover(view);
                        }
                    }
                });

                ImageView user_header = (ImageView) view.findViewById(R.id.user_header);
                TextView title = (TextView) view.findViewById(R.id.title);
                View line = (View) view.findViewById(R.id.line);
                line.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());

                if (smartRankCoverModel != null){
                    Glide.with(SmartRankActivity.this).load(smartRankCoverModel.getCover()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(cover);
                    Glide.with(SmartRankActivity.this).load(smartRankCoverModel.getHeader()).transform(new CircleTransform(SmartRankActivity.this)).dontAnimate().into(user_header);
                    title.setText(smartRankCoverModel.getTitle());
                }

                if (smartRankMyInfo != null){
                    LinearLayout like_layout = (LinearLayout) view.findViewById(R.id.like_layout);
                    TextView like_count = (TextView) view.findViewById(R.id.like_count);
                    ImageView like_icon = (ImageView) view.findViewById(R.id.like_icon);
                    ImageView header = (ImageView) view.findViewById(R.id.header);
                    TextView smart_value = (TextView) view.findViewById(R.id.smart_value);
                    TextView name = (TextView) view.findViewById(R.id.name);
                    TextView detail = (TextView) view.findViewById(R.id.detail);
                    TextView tip = (TextView) view.findViewById(R.id.tip);

                    tip.setText(smartRankMyInfo.getTip());
                    tip.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
                    tip.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());

                    name.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                    name.setText(CustomApplication.loginModel.getNickname());

                    detail.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());
                    detail.setText(smartRankMyInfo.getDetail());

                    ImageView privateIcon = (ImageView) view.findViewById(R.id.private_icon);
                    if (!CommonUtils.isEmpty(smartRankMyInfo.getPrivateIcon())){
                        privateIcon.setVisibility(View.VISIBLE);
                        try{
                            Glide.with(SmartRankActivity.this).load(smartRankMyInfo.getPrivateIcon()).transform(new CircleTransform(SmartRankActivity.this)).dontAnimate().into(privateIcon);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("智慧排行加载图片报错："+e.getLocalizedMessage());
                        }
                    }else {
                        privateIcon.setVisibility(View.GONE);
                    }
                    ImageView lang_member_icon = (ImageView) view.findViewById(R.id.lang_member_icon);
                    if (!CommonUtils.isEmpty(smartRankMyInfo.getMemberIcon())){
                        lang_member_icon.setVisibility(View.VISIBLE);
                        try{
                            Glide.with(SmartRankActivity.this).load(smartRankMyInfo.getMemberIcon()).
                                    diskCacheStrategy(DiskCacheStrategy.RESOURCE).fitCenter().centerCrop().into(lang_member_icon);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("智慧排行加载图片报错："+e.getLocalizedMessage());
                        }

                    }else {
                        lang_member_icon.setVisibility(View.INVISIBLE);
                    }

                    Glide.with(SmartRankActivity.this).load(CustomApplication.loginModel.getHeader()).transform(new CircleTransform(SmartRankActivity.this)).dontAnimate().into(header);

                    smart_value.setText(smartRankMyInfo.getSmartValue()+"");
                    if (smartRankMyInfo.isHighcolor() == true){
                        smart_value.setTextColor(Color.parseColor("#af7820"));
                    }else {
                        smart_value.setTextColor(Color.parseColor("#99cc00"));
                    }

                    if (smartRankMyInfo.getLikeCount() > 0){
                        like_icon.setImageResource(R.drawable.workgroup_img_like_sel);
                    }else {
                        like_icon.setImageResource(R.drawable.workgroup_img_like);
                    }
                    like_count.setText(CommonUtils.getDisplayCount(smartRankMyInfo.getLikeCount()));

                    like_layout.setClickable(true);
                    like_layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            SmartRankModel model = new SmartRankModel();
                            model.setRankid(smartRankMyInfo.getRankid());
                            Intent intent = new Intent(SmartRankActivity.this, SmartRankLikeActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", model);//序列化
                            intent.putExtras(bundle);//发送数据
                            startActivity(intent);
                        }
                    });

                    View like_view = (View) view.findViewById(R.id.like_view);
                    like_view.setClickable(true);
                    like_view.setTag(position);
                    like_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            SmartRankModel model = new SmartRankModel();
                            model.setRankid(smartRankMyInfo.getRankid());
                            Intent intent = new Intent(SmartRankActivity.this, SmartRankLikeActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", model);//序列化
                            intent.putExtras(bundle);//发送数据
                            startActivity(intent);
                        }
                    });

                }
                return view;
            }

            if (convertView == null) {
                convertView = View.inflate(SmartRankActivity.this, R.layout.smart_rank_item,null);
            }
            view=convertView;
            position -= 1;
            SmartRankModel model = list.get(position);

            RelativeLayout smart_rank_item = (RelativeLayout) view.findViewById(R.id.smart_rank_item);
            smart_rank_item.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            TextView rank_no = (TextView) view.findViewById(R.id.rank_no);
            ImageView rank_icon = (ImageView) view.findViewById(R.id.rank_icon);
            LinearLayout like_layout = (LinearLayout) view.findViewById(R.id.like_layout);
            TextView like_count = (TextView) view.findViewById(R.id.like_count);
            ImageView like_icon = (ImageView) view.findViewById(R.id.like_icon);
            ImageView header = (ImageView) view.findViewById(R.id.header);
            TextView smart_value = (TextView) view.findViewById(R.id.smart_value);
            TextView name = (TextView) view.findViewById(R.id.name);

            ImageView privateIcon = (ImageView) view.findViewById(R.id.private_icon);
            if (!CommonUtils.isEmpty(model.getPrivateIcon())){
                privateIcon.setVisibility(View.VISIBLE);
                try{
                    Glide.with(SmartRankActivity.this).load(model.getPrivateIcon()).transform(new CircleTransform(SmartRankActivity.this)).dontAnimate().into(privateIcon);
                }catch (Exception e){
                    LangPublishActivity.uploadError("智慧排行加载图片报错："+e.getLocalizedMessage());
                }
            }else {
                privateIcon.setVisibility(View.GONE);
            }
            ImageView lang_member_icon = (ImageView) view.findViewById(R.id.lang_member_icon);
            if (!CommonUtils.isEmpty(model.getMemberIcon())){
                lang_member_icon.setVisibility(View.VISIBLE);
                try{
                    Glide.with(SmartRankActivity.this).load(model.getMemberIcon()).
                            diskCacheStrategy(DiskCacheStrategy.RESOURCE).fitCenter().centerCrop().into(lang_member_icon);
                }catch (Exception e){
                    LangPublishActivity.uploadError("智慧排行加载图片报错："+e.getLocalizedMessage());
                }

            }else {
                lang_member_icon.setVisibility(View.INVISIBLE);
            }

            if (position == 0){
                rank_icon.setVisibility(View.VISIBLE);
                rank_no.setVisibility(View.INVISIBLE);
                rank_icon.setImageResource(R.drawable.no1);
            }else if (position == 1){
                rank_icon.setVisibility(View.VISIBLE);
                rank_no.setVisibility(View.INVISIBLE);
                rank_icon.setImageResource(R.drawable.no2);
            }else if (position == 2){
                rank_icon.setVisibility(View.VISIBLE);
                rank_no.setVisibility(View.INVISIBLE);
                rank_icon.setImageResource(R.drawable.no3);
            }else {
                rank_icon.setVisibility(View.INVISIBLE);
                rank_no.setVisibility(View.VISIBLE);
            }

            if (position < 10){
                rank_no.setTextColor(Color.parseColor("#af7820"));
            }else {
                rank_no.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            }
            name.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

            if (model.isHighcolor() == true){
                smart_value.setTextColor(Color.parseColor("#af7820"));
            }else {
                smart_value.setTextColor(Color.parseColor("#99cc00"));
            }

            like_count.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());

            rank_no.setText(model.getPm()+"");
            like_count.setText(CommonUtils.getDisplayCount(model.getLikeCount()));
            smart_value.setText(model.getSmartValue()+"");
            name.setText(model.getNickname());
            if (model.isLiked() == true){
                like_icon.setImageResource(R.drawable.workgroup_img_like_sel);
                like_count.setTextColor(Color.parseColor("#99cc00"));
            }else {
                like_icon.setImageResource(R.drawable.workgroup_img_like);
                like_count.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());
            }
            like_layout.setTag(position);
            like_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     onClickLike(view);
                }
            });
            smart_value.setTag(position);
            smart_value.setClickable(true);
            smart_value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickLike(view);
                }
            });

            View like_view = (View) view.findViewById(R.id.like_view);
            like_view.setClickable(true);
            like_view.setTag(position);
            like_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickLike(view);
                }
            });

            Glide.with(SmartRankActivity.this).load(model.getHeader()).transform(new CircleTransform(SmartRankActivity.this)).dontAnimate().into(header);

            return view;
        }
    }

    private void onClickLike(View view){
        int position = (int)view.getTag();
        if (list != null && list.size() > position){
            SmartRankModel model = list.get(position);
            if (model.getUserid().equals(CustomApplication.loginModel.getUid())){
                Intent intent = new Intent(SmartRankActivity.this, SmartRankLikeActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("model", model);//序列化
                intent.putExtras(bundle);//发送数据
                startActivity(intent);
                return;
            }

            if (model.isLiked() == false){
                model.setLiked(true);
                model.setLikeCount(model.getLikeCount()+1);
            }else {
                model.setLiked(false);
                if (model.getLikeCount() > 0){
                    model.setLikeCount(model.getLikeCount()-1);
                }
            }
            adapter.notifyDataSetChanged();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Map<String,String> map = new HashMap<String, String>();
                    map.put("dataId",model.getRankid());
                    map.put("token",CustomApplication.loginModel.getToken());
                    String url = "rank/smart/like.do";
                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                        }

                        @Override
                        public void onError(String msg) {

                        }
                    });
                }
            }).start();

        }

    }

    private void clickCover(View view){

        final List<String> menu = new ArrayList<String>();
        menu.add("赞这个封面");
        menu.add("进入Ta的主页");
        if (smartRankCoverModel.isCare() == false){
            menu.add("关注Ta");
        }

        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(SmartRankActivity.this).builder()
                .setTitle("")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);

        for (String m : menu){
            actionSheetDialog.addSheetItem(m, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which == 1){

                        ToastUtil.show(SmartRankActivity.this, "谢谢，喜欢点赞的人很可爱哦~");

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                final Map<String,String> map = new HashMap<String, String>();
                                map.put("dataId",CommonUtils.formatString(rankid));
                                map.put("dataUserId", smartRankCoverModel.getUserid());
                                map.put("token",CustomApplication.loginModel.getToken());
                                String url = "rank/smart/like/cover.do";
                                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                                    @Override
                                    public void onSuccess(String data) {

                                    }

                                    @Override
                                    public void onError(String msg) {

                                    }
                                });
                            }
                        }).start();
                    }else if (which == 2){
                        if (smartRankCoverModel != null){
                            //进入个人主页
                            Intent intent = new Intent(SmartRankActivity.this, UserLangActivity.class);
                            intent.putExtra(APIKey.useridKey,smartRankCoverModel.getUserid());
                            intent.putExtra(APIKey.typeKey,1);
                            intent.putExtra(APIKey.titleKey,smartRankCoverModel.getName());
                            startActivity(intent);
                        }
                    }else if (which == 3){
                        if (smartRankCoverModel != null){
                            addCare();
                        }
                    }
                }
            });
        }

        actionSheetDialog.show();
    }

    private void addCare(){

        if (smartRankCoverModel != null && smartRankCoverModel.isCare() == false){
            smartRankCoverModel.setCare(true);
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put("authorid", smartRankCoverModel.getUserid());
            String url = "miniapp/"+"langappid"+"/addcare.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        ToastUtil.show(SmartRankActivity.this, "关注成功");
                    }else {
                        CustomApplication.showTip(commonModel, SmartRankActivity.this);
                    }
                }

                @Override
                public void onError(String msg) {
                }
            });
        }
    }

}
